//
//  DetailStoryTableViewController.m
//  MyItem
//
//  Created by SoulWater on 16/1/7.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "DetailStoryTableViewController.h"
#import "DetailStoryCell.h"
#import "DetailHeadView.h"
@interface DetailStoryTableViewController ()
{
    NSArray *_dataArray;

}

@end

@implementation DetailStoryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"故事详情";
    
    //分割线取消
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = backButton;
    
    UIBarButtonItem *sharButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(sharAction)];
    self.navigationItem.rightBarButtonItem = sharButton;
    
   
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    //注册头视图
   // [self.tableView registerNib:[UINib nibWithNibName:@"DetailHeadView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"DetailHeadView"];
    
    _headView = [[[NSBundle mainBundle]loadNibNamed:@"DetailHeadView" owner:self options:nil]lastObject];
    _headView.storyModel = self.storyModel;

    _headView.frame = CGRectMake(0, 0, HScreenWidth, _headView.viewHeight);
    self.tableView.tableHeaderView = _headView;
    

}
-(void)backAction {

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

-(void)sharAction {

    NSLog(@"分享。。。。。。。");
}

-(void)setStoryModel:(StoryModel *)storyModel
{
    if (_storyModel != storyModel) {
        
        _storyModel = storyModel;
        
        _dataArray = self.storyModel.wareArray;
        
        _headView.storyModel = self.storyModel;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailStory"];
    
    if (!cell) {
        
        cell = [[DetailStoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellStyleDefault"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    cell.wareModel = _dataArray[indexPath.row];

    return cell;
}

//单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
    NSDictionary *dic = self.storyModel.wareInfoList[indexPath.row];
    wareInfoModel *wareModel = [[wareInfoModel alloc]initWithDataDic:dic];

    if (wareModel.imgText != nil){
        
        NSDictionary *attribut = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
        CGRect rect = [wareModel.imgText boundingRectWithSize:CGSizeMake(HScreenWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribut context:NULL];
        height = rect.size.height + HScreenWidth/3*2;
    }
    else{
        
        height = HScreenWidth/3*2;
    }
    
    return  height + 10;
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//
//    return _headView;
//}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
